package com.da.web.dorm_storey.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.da.common.Result;
import com.da.web.dorm_storey.entity.DormStorey;
import com.da.web.dorm_storey.service.DormStoreyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author da
 * @time 2023/12/9 上午 10:45
 */
@RestController
@RequestMapping("/api/storey")
public class DormStoreyController {
    @Autowired
    private DormStoreyService dormStoreyService;

    //新增层数
    @PostMapping
    public Result add(@RequestBody DormStorey dormStorey) {
        dormStoreyService.add(dormStorey);
        return Result.success("新增成功");
    }

    //编辑层数
    @PutMapping
    public Result edit(@RequestBody DormStorey dormStorey) {
        dormStoreyService.edit(dormStorey);
        return Result.success("编辑成功");
    }

    //通过id删除层数
    @DeleteMapping("/{buildId}/{storeyId}")
    public Result delete(@PathVariable("buildId") Long buildId, @PathVariable("storeyId") Long storeyId) {
        //还需要判断层下面有没有宿舍,有宿舍就不能删除
        dormStoreyService.delete(buildId, storeyId);
        return Result.success("删除成功");
    }

    //根据楼栋id查询对应的层数
    @GetMapping("/getStoryList")
    public Result getStoryList(Long buildId) {
        QueryWrapper<DormStorey> query = new QueryWrapper<>();
        query.lambda().eq(DormStorey::getBuildId, buildId);
        List<DormStorey> list = dormStoreyService.list(query);
        return Result.success("查询成功", list);
    }

    //初始化床位
    @PostMapping("/initBed")
    public Result initBed(@RequestBody DormStorey dormStorey) {
        dormStoreyService.initBed(dormStorey);
        return Result.success("初始化床位成功");
    }

}
